package com.cy.dda.service.goodsdiligencing.impl;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingMaintainReportAttachmentMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingMaintainReportAuditRecordMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingMaintainReportMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingMapper;
import com.cy.dda.dao.mapper.user.UserMapper;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.goodsdiligencing.GoodsDiligencingReportAuditAO;
import com.cy.dda.model.ao.goodsdiligencing.MaintainReportSaveAO;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencing;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingMaintainReport;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingMaintainReportAttachment;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingMaintainReportAuditRecord;
import com.cy.dda.model.domain.user.User;
import com.cy.dda.model.enums.diligencing.DiligencingReportAuditResult;
import com.cy.dda.model.enums.diligencing.DiligencingStatus;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingAttachmentType;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingStatus;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingType;
import com.cy.dda.model.vo.goodsdiligencing.GoodsDiligencingReportAttachmentVO;
import com.cy.dda.model.vo.goodsdiligencing.GoodsDiligencingReportAuditRecordVO;
import com.cy.dda.model.vo.goodsdiligencing.MaintainReportAdminDetailVO;
import com.cy.dda.model.vo.goodsdiligencing.MaintainReportDetailVO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.goodsdiligencing.MaintainReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description 维护报告 接口实现类
 * @Author tanyilin
 * @Date 2019/8/28 17:34
 */
@Service
public class MaintainReportServiceImpl extends BaseService implements MaintainReportService {

    @Autowired
    private GoodsDiligencingMapper                          goodsDiligencingMapper;
    @Autowired
    private GoodsDiligencingMaintainReportMapper            maintainReportMapper;
    @Autowired
    private GoodsDiligencingMaintainReportAttachmentMapper  maintainReportAttachmentMapper;
    @Autowired
    private GoodsDiligencingMaintainReportAuditRecordMapper maintainReportAuditRecordMapper;
    @Autowired
    private UserMapper                                      userMapper;

    @Override
    public MaintainReportDetailVO getMaintainReportDetail(Long goodsDiligencingId) {

        GoodsDiligencingMaintainReport maintainReport = new GoodsDiligencingMaintainReport();
        maintainReport.setGoodsDiligencingId(goodsDiligencingId);
        maintainReport = maintainReportMapper.selectOne(maintainReport);
        if(maintainReport != null) {
            //维护报告已填写
            MaintainReportDetailVO detailVO = new MaintainReportDetailVO();
            BeanUtils.copyProperties(maintainReport, detailVO);
            detailVO.setMaintainReportId(maintainReport.getId());

            //查询家具照片图片
            List<GoodsDiligencingReportAttachmentVO> furnitureImages = maintainReportAttachmentMapper.selectMaintainReportAttachment(maintainReport.getId(), GoodsDiligencingAttachmentType.FURNITURE_IMAGE);
            //查询现场在场图片
            List<GoodsDiligencingReportAttachmentVO> sceneImages = maintainReportAttachmentMapper.selectMaintainReportAttachment(maintainReport.getId(), GoodsDiligencingAttachmentType.SCENE_IMAGE);

            detailVO.setFurnitureImages(furnitureImages);
            detailVO.setSceneImages(sceneImages);

            return detailVO;

        }

        return null;
    }

    @Override
    @Transactional
    public Result<Map<String, Object>> saveMaintainReport(MaintainReportSaveAO saveAO, Long userId) {

        Date time = new Date();
        Long goodsDiligencingId = saveAO.getGoodsDiligencingId();
        Long maintainReportId = saveAO.getMaintainReportId();

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(goodsDiligencingId);
        if(!(GoodsDiligencingType.MAINTAIN_INVESTIGATE.equals(dbGoodsDiligencing.getDiligencingType())
                //&& DiligencingStatus.PROCESSING.equals(dbGoodsDiligencing.getDiligencingStatus())
                && GoodsDiligencingStatus.MAINTAIN_ING.equals(dbGoodsDiligencing.getStatus()))) {
            return Result.fail("当前尽调阶段不可提交维护调查报告");
        }

        if(CommonUtils.isEmpty(maintainReportId)) {
            //维护报告ID为空，则说明还未提交过维护报告，则新建一个维护报告
            GoodsDiligencingMaintainReport newMaintainReport = new GoodsDiligencingMaintainReport();
            BeanUtils.copyProperties(saveAO, newMaintainReport);
            newMaintainReport.setGoodsDiligencingId(goodsDiligencingId);
            newMaintainReport.setUserId(userId);
            newMaintainReport.setCreateTime(time);
            newMaintainReport.setUpdateTime(time);
            maintainReportMapper.insertSelective(newMaintainReport);
            maintainReportId = newMaintainReport.getId();
        }else {
            //维护报告ID不为空，说明维护报告已存在，只需更新
            GoodsDiligencingMaintainReport updateMaintainReport = new GoodsDiligencingMaintainReport();
            BeanUtils.copyProperties(saveAO, updateMaintainReport);
            updateMaintainReport.setId(maintainReportId);
            updateMaintainReport.setUserId(userId);
            updateMaintainReport.setUpdateTime(time);
            maintainReportMapper.updateByPrimaryKeySelective(updateMaintainReport);
        }

        //保存家具照片图片
        List<GoodsDiligencingReportAttachmentVO> furnitureImages = saveAO.getFurnitureImages();
        if(!CommonUtils.isEmpty(furnitureImages)) {
            List<GoodsDiligencingMaintainReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentVO bean : furnitureImages) {
                GoodsDiligencingMaintainReportAttachment reportAttachment = new GoodsDiligencingMaintainReportAttachment();
                BeanUtils.copyProperties(bean, reportAttachment);
                reportAttachment.setMaintainReportId(maintainReportId);
                reportAttachment.setCreateTime(time);
                reportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(reportAttachment);
            }
            //批量新增处理
            maintainReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            maintainReportAttachmentMapper.deleteByUpdateTimeAndType(maintainReportId, GoodsDiligencingAttachmentType.FURNITURE_IMAGE, time);
        }

        //保存现场在场图片
        List<GoodsDiligencingReportAttachmentVO> sceneImages = saveAO.getSceneImages();
        if(!CommonUtils.isEmpty(sceneImages)) {
            List<GoodsDiligencingMaintainReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentVO bean : sceneImages) {
                GoodsDiligencingMaintainReportAttachment reportAttachment = new GoodsDiligencingMaintainReportAttachment();
                BeanUtils.copyProperties(bean, reportAttachment);
                reportAttachment.setMaintainReportId(maintainReportId);
                reportAttachment.setCreateTime(time);
                reportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(reportAttachment);
            }
            //批量新增处理
            maintainReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            maintainReportAttachmentMapper.deleteByUpdateTimeAndType(maintainReportId, GoodsDiligencingAttachmentType.SCENE_IMAGE, time);
        }

        //更改尽调报告状态为‘待确认’
        if(DiligencingStatus.PROCESSING.equals(dbGoodsDiligencing.getDiligencingStatus()) || DiligencingStatus.REPORT_RETURN.equals(dbGoodsDiligencing.getDiligencingStatus())) {
            GoodsDiligencing updateGoodsDiligencing = new GoodsDiligencing();
            updateGoodsDiligencing.setId(dbGoodsDiligencing.getId());
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.REPORT_CONFIRM_WAIT);
            updateGoodsDiligencing.setConclusion(saveAO.getConclusion());
            updateGoodsDiligencing.setUpdateTime(time);
            goodsDiligencingMapper.updateByPrimaryKeySelective(updateGoodsDiligencing);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("goodsDiligencingId", goodsDiligencingId);
        map.put("maintainReportId", maintainReportId);

        return Result.success("保存成功", map);
    }

    @Override
    @Transactional
    public void doAuditMaintainReport(GoodsDiligencingReportAuditAO auditAO, Long managerId) {

        Date time = new Date();
        Long goodsDiligencingId = auditAO.getGoodsDiligencingId();

        //先查询在库的 维护报告
        GoodsDiligencingMaintainReport maintainReport = new GoodsDiligencingMaintainReport();
        maintainReport.setGoodsDiligencingId(goodsDiligencingId);
        maintainReport = maintainReportMapper.selectOne(maintainReport);
        state(maintainReport != null, "数据异常，该尽调报告不存在");

        //添加审核记录
        GoodsDiligencingMaintainReportAuditRecord auditRecord = new GoodsDiligencingMaintainReportAuditRecord();
        auditRecord.setMaintainReportId(maintainReport.getId());
        auditRecord.setResult(auditAO.getResult());
        auditRecord.setRemark(auditAO.getRemark());
        auditRecord.setManagerId(managerId);
        auditRecord.setCreateTime(time);
        auditRecord.setUpdateTime(time);
        maintainReportAuditRecordMapper.insertSelective(auditRecord);

        //更改 家具尽调 的状态
        GoodsDiligencing updateGoodsDiligencing = new GoodsDiligencing();
        updateGoodsDiligencing.setId(goodsDiligencingId);
        if(DiligencingReportAuditResult.PASS.equals(auditAO.getResult())) {
            //通过，说明 已维护
            updateGoodsDiligencing.setDiligencingType(GoodsDiligencingType.MAINTAIN_INVESTIGATE);
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.COMPLETED);
            updateGoodsDiligencing.setStatus(GoodsDiligencingStatus.ALREADY_MAINTAIN);
        }else if(DiligencingReportAuditResult.RETURN.equals(auditAO.getResult())) {
            //退回，说明还是 维护中
            updateGoodsDiligencing.setDiligencingType(GoodsDiligencingType.MAINTAIN_INVESTIGATE);
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.REPORT_RETURN);
            updateGoodsDiligencing.setStatus(GoodsDiligencingStatus.MAINTAIN_ING);
        }
        updateGoodsDiligencing.setUpdateTime(time);
        goodsDiligencingMapper.updateByPrimaryKeySelective(updateGoodsDiligencing);

    }

    @Override
    public MaintainReportAdminDetailVO getMaintainReportAdminDetail(Long goodsDiligencingId) {

        //先查询在库的 维护报告
        GoodsDiligencingMaintainReport maintainReport = new GoodsDiligencingMaintainReport();
        maintainReport.setGoodsDiligencingId(goodsDiligencingId);
        maintainReport = maintainReportMapper.selectOne(maintainReport);
        if(maintainReport != null) {
            //维护报告已存在
            MaintainReportAdminDetailVO detailVO = new MaintainReportAdminDetailVO();
            BeanUtils.copyProperties(maintainReport, detailVO);

            //查找调查员
            User user = userMapper.selectByPrimaryKey(maintainReport.getUserId());
            detailVO.setInvestigatorName(user.getName());

            //查询报告审核记录
            List<GoodsDiligencingReportAuditRecordVO> reportAuditRecordList = maintainReportAuditRecordMapper.selectMaintainReportAuditRecord(maintainReport.getId());
            detailVO.setReportAuditRecordList(reportAuditRecordList);

            //查询报告图片
            List<GoodsDiligencingReportAttachmentVO> reportAttachmentList = maintainReportAttachmentMapper.selectMaintainReportAttachment(maintainReport.getId(), null);
            detailVO.setReportAttachmentList(reportAttachmentList);

            return detailVO;
        }

        return new MaintainReportAdminDetailVO();
    }


}
